// pages/posts/movies/movie.js
var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inTheater: {},
    comingSoon: {},
    top250: {},
    containerShow: true,
    searchPanelShow: false,
    searchResult: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //加载电影从豆瓣上获取的数据--这里操作很简单，封装路径，调用方法h偶去数据
    var inTheatersUrl = app.globalData.doubanBase + "/v2/movie/in_theaters" + "?start=0&count=3";
    var comingSoonUrl = app.globalData.doubanBase + "/v2/movie/coming_soon" + "?start=0&count=3";
    var top250Url = app.globalData.doubanBase + "/v2/movie/top250" + "?start=0&count=3";

    //调用获取数据
    this.getMovieListData(inTheatersUrl, "inTheater", "正在热映");
    this.getMovieListData(comingSoonUrl, "comingSoon", "即将上映");
    this.getMovieListData(top250Url, "top250", "top250");
  },
  getMovieListData: function(url, settedKey, categoryTitle) {
    var that = this;
    //请求服务器获取数据，request方法，和ajax很相似
    wx.request({
      url: url,
      data: {},
      method: 'GET',
      header: {
        "Content-Type": "application/xml"
      },
      success: function(res) {
        //请求成功后的方法--这里主要是要将数据绑定到data中
        //console.log('成功');
        //console.dir(res.data);
        that.processDoubanData(res.data, settedKey, categoryTitle);
      },
      fail: function(error) {
        // 请求失败的跳转方法
        console.log(11)
      }
    })

  },
  //将对应的电影绑定到data中
  processDoubanData: function(moviesDouban, settedKey, categoryTitle) {
    //将数据封装成一个数字对象的json
    var movies = [];
    for (var idx in moviesDouban.subjects) {
      //得到对应的该项获取值
      var subject = moviesDouban.subjects[idx];
      var title = subject.title;
      if (title.length >= 6) {
        title = title.substring(0, 6) + "...";
      }
      var temp = {
        stars: util.convertToStarsArray(subject.rating.stars),
        title: title,
        average: subject.rating.average,
        coverageUrl: subject.images.large,
        movieId: subject.id
      }
      //将值push到数组中
      movies.push(temp);
    }
    console.log(movies);
    //将数据绑定到data中
    var readyData = {};
    readyData[settedKey] = {
      categoryTitle: categoryTitle,
      movies: movies
    };
    //console.dir(readyData);
    this.setData(readyData);
  },

  //点击更多后事件处理函数
  onMoreTap: function(event) {
    //点击了更多按钮之后要跳转页面，携带一个参数是categoryTitle
    var category = event.currentTarget.dataset.category;
    wx.navigateTo({
      url: 'more-movie/more-movie?category=' + category,
    });
  },
  onBindFocus: function(event) {
    //焦点进来之后需要隐藏正在热映即将上映top250--通过设置状态
    this.setData({
        containerShow: false,
        searchPanelShow: true
      })
  },
  onBindBlur:function(event){
    //当失去焦点或者点击回车之后需要从服务器访问该关键字对应的电影
    var keyword = event.detail.value;
    var queryUrl = app.globalData.doubanBase + "/v2/movie/search?q=" + keyword;
    this.getMovieListData(queryUrl,"searchResult","")
  },
  onCancelImgTap:function(event){
    this.setData({
      containerShow: true,
      searchPanelShow: false
    })
  },
  // onReachBottom:function(event){
  //   //上划加载更多数据
  //   console.log('aaa');
  // },
  onMovieTap:function(event){
    //这是点击弹出电影详情页面
    var movieId = event.currentTarget.dataset.movieid;
    // console.log(movieId)
    wx.navigateTo({
      url: 'movie-detail/movie-detail?id=' + movieId,
    })
  }
})